package gdut.jmxywz.action;

import gdut.jmxywz.been.MessageBoard;
import gdut.jmxywz.been.MessageBoardFile;
import gdut.jmxywz.been.MessageBoardReply;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
public class TeacherMessageBoardAction extends BaseAction {
	
	private  static final String  TEACHER_SESSION_KEY = "teacherSessionKey";
	
	private MessageBoard teacherMessageBoard ;
	
	private MessageBoardReply  messageReply;
	
	private MessageBoardFile  messageReplyFile;
	
	private  Teacher teacher;
	//文件
	private String[] files_content;
	private String[] files_title;
	private List<File> upload_file;
	private String[] files_save_as;
	private String[] upload_fileContentType;
	private String[] upload_fileFileName;
	private String   savePath;
	
	private List<MessageBoardFile>  messageBoardFile;
	
	private List<MessageBoardReply> messageBoardReply;
	
	//分页
	private int        currentPage=1;
	private int        pageSize=5;
	private String     nextPage;
	private PageModel  pm;
	
	//查询条件
	private  String   where_search=""; 
	private  String   where_value;
	private  String   like ;

	//删除所有回复
	private String   replyAll;
	private String   auditing;
	
	
	public MessageBoard getTeacherMessageBoard() {
		return teacherMessageBoard;
	}
	
	public void setTeacherMessageBoard(MessageBoard teacherMessageBoard) {
		this.teacherMessageBoard = teacherMessageBoard;
	}
	
	public String[] getFiles_content() {
		return files_content;
	}

	public void setFiles_content(String[] filesContent) {
		files_content = filesContent;
	}

	public String[] getFiles_title() {
		return files_title;
	}

	public void setFiles_title(String[] filesTitle) {
		files_title = filesTitle;
	}

	public List<File> getUpload_file() {
		return upload_file;
	}

	public void setUpload_file(List<File> uploadFile) {
		upload_file = uploadFile;
	}

	public String[] getFiles_save_as() {
		return files_save_as;
	}

	public void setFiles_save_as(String[] filesSaveAs) {
		files_save_as = filesSaveAs;
	}

	public String[] getUpload_fileContentType() {
		return upload_fileContentType;
	}

	public void setUpload_fileContentType(String[] uploadFileContentType) {
		upload_fileContentType = uploadFileContentType;
	}

	public String[] getUpload_fileFileName() {
		return upload_fileFileName;
	}

	public void setUpload_fileFileName(String[] uploadFileFileName) {
		upload_fileFileName = uploadFileFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public List<MessageBoardFile> getMessageBoardFile() {
		return messageBoardFile;
	}

	public void setMessageBoardFile(List<MessageBoardFile> messageBoardFile) {
		this.messageBoardFile = messageBoardFile;
	}
     
	
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getNextPage() {
		return nextPage;
	}

	public void setNextPage(String nextPage) {
		this.nextPage = nextPage;
	}

	public PageModel getPm() {
		return pm;
	}

	public void setPm(PageModel pm) {
		this.pm = pm;
	}
	

	
	public String getWhere_search() {
		return where_search;
	}

	public void setWhere_search(String whereSearch) {
		where_search = whereSearch;
	}

	public String getWhere_value() {
		return where_value;
	}

	public void setWhere_value(String whereValue) {
		where_value = whereValue;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public List<MessageBoardReply> getMessageBoardReply() {
		return messageBoardReply;
	}

	public void setMessageBoardReply(List<MessageBoardReply> messageBoardReply) {
		this.messageBoardReply = messageBoardReply;
	}
	
	

	public MessageBoardReply getMessageReply() {
		return messageReply;
	}

	public void setMessageReply(MessageBoardReply messageReply) {
		this.messageReply = messageReply;
	}

	
	public MessageBoardFile getMessageReplyFile() {
		return messageReplyFile;
	}

	public void setMessageReplyFile(MessageBoardFile messageReplyFile) {
		this.messageReplyFile = messageReplyFile;
	}
	
	public String getReplyAll() {
		return replyAll;
	}

	public void setReplyAll(String replyAll) {
		this.replyAll = replyAll;
	}

	public String getAuditing() {
		return auditing;
	}

	public void setAuditing(String auditing) {
		this.auditing = auditing;
	}
	

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	/***
	 *  发布留言
	 * @return
	 * @throws Exception
	 */
	public String Add() throws Exception{
		
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		
	    List<File> files=getUpload_file();
		
		List file_list = new ArrayList();
		
		setSavePath("");//上传路径
		
		boolean  b = false;
		boolean  b2 =false;
		
		System.out.println(teacherMessageBoard.getMessageContext());
		teacherMessageBoard.setTeacher(teacher);
		
		b=this.getCommonService().saveObject(teacherMessageBoard);
		
		//处理文件上传

		if(files!=null)
			for(int i=0;i<files.size();i++){//控制上传部分

				String extend_name=getUpload_fileFileName()[i];
				System.out.println(extend_name);
				int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
				extend_name=extend_name.substring(extend_name_int);
							
				java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
			   String datestr = df.format(new java.util.Date()) ;
			   Random rand = new Random();//生成随机数
			   int ram = rand.nextInt(500);
				String ram_str = String.valueOf(ram);
				for(int i_i=0; ram_str.length()<6;i_i++){
					ram_str="0"+ram_str;
				}
				
				extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
				String Save_path="\\messageBoardFiles\\"+extend_name;//文件在服务器保存的相对路径
				
				FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
				
				FileInputStream fis=new FileInputStream(files.get(i));
				byte[] buffer=new byte[1024];
				int len=0;
				while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
				
				}
				
			    MessageBoardFile mfile = new MessageBoardFile();
				
				System.out.println();
				System.out.println("----------第--"+(i+1)+"--文件--------------");
				System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
				System.out.println("--------文件名:"+files_title[i]+"--------------");
				System.out.println("--------文件说明:"+files_content[i]+"--------------");
				
				mfile.setFileUrl(Save_path);
				mfile.setFileName(files_title[i]);
				mfile.setFileContext(files_content[i]);
				mfile.setMessageBoard(teacherMessageBoard);
				mfile.setFileDownTime(0);
				
				//插入附件表
				b2=this.getCommonService().saveObject(mfile);
		
		//文件上传处理结束
		
     	}
		
		if(b){
			return "TeacherMessageBoardAdd";
		}else{
			return "error";
		}
	
	}
	
	/***
	 *  显示所有帖子
	 * @return
	 * @throws Exception
	 */
	public String ListAll()throws Exception{
		
		String hql ="from MessageBoard where auditing in(1,2)";
		
		nextPage = "teacherMessageBoardListAll.action";
		
 	  if(!"".equals(where_value)&&where_value!=null){
    		  
    		  System.out.println("++++-- "+where_value);
    		  if(like.equals("like"))
    		  {
             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
    		  }else{
    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
    		  }
    		  
    	  }
       hql = hql +" order by messageBoardTime desc,messageBoardid desc";
		
       System.out.println("====="+hql);
		
		pm=(PageModel) this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
		
		
		return "TeacherMessageBoardListAll";
	}
	
	
public String ListAll1()throws Exception{
		
		String hql ="from MessageBoard where auditing in(1,2)";
		
		nextPage = "teacherMessageBoardListAll1.action";
		
 	  if(!"".equals(where_value)&&where_value!=null){
    		  
    		  System.out.println("++++-- "+where_value);
    		  if(like.equals("like"))
    		  {
             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
    		  }else{
    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
    		  }
    		  
    	  }
       hql = hql +" order by messageBoardTime desc,messageBoardid desc";
		
       System.out.println("====="+hql);
		
		pm=(PageModel) this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
		
		
		return "TeacherMessageBoardListAll";
	}
	
	/***
	 * 留言详细
	 * @return
	 * @throws Exception
	 */
	 public String Detail() throws Exception{
		 

		 System.out.println("--id--"+teacherMessageBoard.getMessageBoardid());
		 
		 String hql ="from MessageBoardFile mf where  mf.auditing in(1,2) and mf.messageBoard.messageBoardid="+teacherMessageBoard.getMessageBoardid();
		 
		 String hql2="from MessageBoardReply mr where mr.auditing in(1,2) and mr.messageBoard.messageBoardid="+teacherMessageBoard.getMessageBoardid(); 
		 
		   hql2 = hql2+" order by messageBoardReplyTime desc,messageBoardReplyId desc";
		   
		 teacherMessageBoard=(MessageBoard)this.getCommonService().findByIntId(teacherMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.MessageBoard");
		 
		 System.out.println(teacherMessageBoard.getMessageContext());
		 
		 
		 //判断是否有附件
           		 
		 messageBoardFile = this.getCommonService().findListByHql(hql, null);
		 
//		 //是否有回复
		 
		 String nextPage = "teacherMessageBoardDetail.action?teacherMessageBoard.messageBoardid="+teacherMessageBoard.getMessageBoardid()+"&";
		 
		 pm = this.getCommonService().findPage(hql2, null, currentPage, pageSize, nextPage, "");
		 
		 System.out.println("============"+pm.getPageMessage());
		 
//		 messageBoardReply = this.getCommonService().findListByHql(hql2, null);
		 
		 
    	// 阅读次数加一
		 
		 teacherMessageBoard.setReadtimes(teacherMessageBoard.getReadtimes()+1);
		 
		 this.getCommonService().updateObject(teacherMessageBoard);
		 
		return "TeacherMessageBoardDetail";
	 }
	 
	     /***
		 * 留言审核详细
		 * @return
		 * @throws Exception
		 */
		 public String AuditingDetail() throws Exception{
			 

			 System.out.println("--id--"+teacherMessageBoard.getMessageBoardid());
			 
			 String hql ="from MessageBoardFile mf where  mf.messageBoard.messageBoardid="+teacherMessageBoard.getMessageBoardid();
			 
			 String hql2="from MessageBoardReply mr where  mr.messageBoard.messageBoardid="+teacherMessageBoard.getMessageBoardid(); 
			 
			   hql2= hql2+ " order by auditing asc,messageBoardReplyTime desc,messageBoardReplyId desc";
			 teacherMessageBoard=(MessageBoard)this.getCommonService().findByIntId(teacherMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.MessageBoard");
			 
			 System.out.println(teacherMessageBoard.getMessageContext());
			 
			 //判断是否有附件
	           		 
			 messageBoardFile = this.getCommonService().findListByHql(hql, null);
			 
//			 //是否有回复
			 
			 String nextPage = "teacherMessageBoardAuditingDetail.action?teacherMessageBoard.messageBoardid="+teacherMessageBoard.getMessageBoardid()+"&";
			 
			 pm = this.getCommonService().findPage(hql2, null, currentPage, pageSize, nextPage, "");
			 
			 System.out.println("============"+pm.getPageMessage());
			 
			return "TeacherMessageBoardAuditingDetail";
		 }
		 
		 public void Reply()throws Exception{
			 
				   teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
				 
				   HttpServletResponse res = ServletActionContext.getResponse();
				    
				    res.setCharacterEncoding("utf-8");
				    
				    res.setContentType("text/xml");
				    
				    System.out.println("-==============================");
				    
				    boolean b=false;
				    
				    PrintWriter out = res.getWriter();   
//					 //默认回复设置为院内
					 messageReply.setAuditing("1");
					 messageReply.setTeacher(teacher);
					 
				    b=this.getCommonService().saveObject(messageReply);
				    
				    try {   
		            out = res.getWriter();   
		            out.print("<root>");   
		            out.print("<message>");   
		            out.print("yes");   
		            out.print("</message>");   
		            out.print("</root>");   
		        } catch (Exception e) {   
		            e.printStackTrace();   
		        } finally {   
		            out.flush();   
		            out.close();   
		        }   
	        
				 
		 }
		 
//	 /***
//	  * 留言回复--
//	  * 异步交互,实现插入回复内容，并同时返回新回复的内容---//同时实现回复的修改(修改新版，不用此功能)
//	  * @return
//	  * @throws Exception
//	  */
//	 public void Reply() throws Exception{
//		    
//		  
//
//		
//	    HttpServletResponse res = ServletActionContext.getResponse();
//	    
//	    res.setCharacterEncoding("utf-8");
//	    
//	    res.setContentType("text/xml");
//		 
//		 boolean b=false;
//	
//		 //未审核
//		 
//		 messageReply.setAuditing("0");
//		 
//	    b=this.getCommonService().saveObject(messageReply);
//	    
//	    
//		String hql = "from MessageBoardReply mbr where mbr.messageBoardReplyId="+messageReply.getMessageBoardReplyId();
//
//		System.out.println("hql---"+hql);
//		
//	    System.out.println("---"+messageReply.getMessageBoardReplyId());
//	    
//	    	
////	    messageReply =(MessageBoardReply)this.getCommonService().findByIntId(messageReply.getMessageBoardReplyId(), "gdut.jmxywz.been.MessageBoardReply");
//	    
//		 
//		 
//		 PrintWriter out = res.getWriter();   
//		  
//	        /***
//	         * 
//	         * XMLUtils
//	         */
//	        Document document = XMLUtils.newDocument();
//	        
//		    Element  rspNode = XMLUtils.addElement(document, "param",new Attr[]{});
////		 XMLUtils.addTextElement(rspNode, "dateStar", null,new Attr[]{XMLUtils.createAttribute(document,"value",String.valueOf(t.getStartDate())),XMLUtils.createAttribute(document,"today",String.valueOf(t.getStartDate().getDay()==0?"7":t.getStartDate().getDay()))});
//
//		    XMLUtils.addTextElement(rspNode,"TeacherBoardId" ,null,new Attr[]{XMLUtils.createAttribute(document,"value",String.valueOf(teacherMessageBoard.getMessageBoardid()))});
//	    	
//		  
////		    PageModel pm = this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
//		    
//		    List listAll = this.getCommonService().findListByHql(hql, null);
//		    
//		 
//		       Iterator it = listAll.iterator();
//		       while(it.hasNext()){
//		    	   String teacher ="匿名者";
//		    	   MessageBoardReply mbr= (MessageBoardReply)it.next();
//			    
//		    	   Element chiElement = XMLUtils.addTextElement(rspNode,"reply", null,null);
//			       
//		    	   if(mbr.getTeacher()!=null){
//		    		   
//		    		   teacher=mbr.getTeacher().getTeacherName();
//		    	   }
//		    	   
//			       XMLUtils.addTextElement(chiElement, "teacher", teacher);
//			       
//			       XMLUtils.addTextElement(chiElement, "replyId", String.valueOf(mbr.getMessageBoardReplyId()));
//			       
//			       XMLUtils.addTextElement(chiElement, "replyAuditing", mbr.getAuditing());
//			       
//			       XMLUtils.addTextElement(chiElement, "replyTime",String.valueOf(mbr.getMessageBoardReplyTime().toString().substring(0,10)));
//			       
//			       XMLUtils.addTextElement(chiElement, "context", mbr.getContext());
//
//		       }
//		       
//		       System.out.println("-----"+XMLUtils.toXMLString(document));
//		       
//		       out.println(XMLUtils.toXMLString(document));
//	        
////		 return "TeacehrMessageBoardReply";
//		       
////		       try {   
////		            out = res.getWriter();   
////		            out.print("<root>");   
////		            out.print("<message>");   
////		            out.print(replyMessage);   
////		            out.print("</message>");   
////		            out.print("</root>");   
////		        } catch (Exception e) {   
////		            e.printStackTrace();   
////		        } finally {   
////		            out.flush();   
////		            out.close();   
////		        }   
//	        
//	 }
//	 
	 
	 /***
	  * 管理员删除留言
	  * @return
	  * @throws Exception
	  */
	 
	 public String DeleteBoard() throws Exception{
		 
		 System.out.println("----delete--------");
		 
		 String hql ="from MessageBoardReply mbr where ";
		
		 
		 //删除留言同时将所有回复删掉
		 if(teacherMessageBoard!=null){
			 if(teacherMessageBoard.getMessageBoardid()!=null){
				 
				 
				 //删除全部回复
				 hql=hql+" mbr.messageBoard.messageBoardid = " + teacherMessageBoard.getMessageBoardid() ;
				
				 List replyList = this.getCommonService().findListByHql(hql, null);
				 
				 Iterator iter = replyList.iterator();
				 
				 while(iter.hasNext()){
					 
					 MessageBoardReply mbr = (MessageBoardReply)iter.next();
					 
					 this.getCommonService().deleteObject(mbr);
					 
					 System.out.println("所有回复删除成功"); 
				 }
				 
				 this.getCommonService().deleteObject(teacherMessageBoard);
				 
			 }
		 }
		 
		 
		 return "TeacherMessageBoardDeleteBoard";
	 }
	 
	 public String DeleteReply() throws Exception{
		 
		 String hql ="from MessageBoardReply mbr where ";
		 
		 //删除全部回复,replyAll==1时为删除所有回复。
		 if(teacherMessageBoard!=null&&"1".equals(replyAll)){
			 
			 if(teacherMessageBoard.getMessageBoardid()!=null){
				 
				hql=hql+" mbr.messageBoard.messageBoardid = " + teacherMessageBoard.getMessageBoardid() ;
				
				System.out.println("----删除所有留言回复-------");
				 
			 }else {
				 hql =hql+" 1=1 ";
			 }
			 
		 }
		 
		 //删除一条回复
		 if(messageReply!=null){	
			 System.out.println(messageReply.getMessageBoardReplyId()+"------"+teacherMessageBoard.getMessageBoardid() );
			 if(messageReply.getMessageBoardReplyId()!=null){
				 
				 hql = hql+" mbr.messageBoardReplyId = "+messageReply.getMessageBoardReplyId();
				 
				 System.out.println("----删除一条留言回复-------");
			 }
		 }
		 
		 System.out.println("hql--"+hql);
		 
		 List replyList = this.getCommonService().findListByHql(hql, null);
		 
		 Iterator iter = replyList.iterator();
		 
		 while(iter.hasNext()){
			 
			 MessageBoardReply mbr = (MessageBoardReply)iter.next();
			 
			 this.getCommonService().deleteObject(mbr);
			 
			 System.out.println("回复删除成功");
		 }
		 
		 
		 return "TeacherMessageBoardDeleteReply";
	 }
	 
	 
	 /***
	  *   修改留言
	  * @return
	  * @throws Exception
	  */

	 
	 public String Modify() throws Exception{
		 
		 System.out.println("++++");
		 
		   List<File> files=getUpload_file();
			
			List file_list = new ArrayList();
			
			setSavePath("");//上传路径
			
			boolean  b = false;
			boolean  b2 =false;
			
			System.out.println(teacherMessageBoard.getMessageBoardid());
			
			System.out.println(teacherMessageBoard.getMessageTitle());
			
			System.out.println(teacherMessageBoard.getMessageContext());
			
			int id =teacherMessageBoard.getMessageBoardid();
			
			MessageBoard mb =(MessageBoard)this.getCommonService().findByIntId(id,"gdut.jmxywz.been.MessageBoard");
			
			mb.setMessageTitle(teacherMessageBoard.getMessageTitle());
			mb.setMessageContext(teacherMessageBoard.getMessageContext());
			mb.setReplylocked(teacherMessageBoard.getReplylocked());
			
			
			b=this.getCommonService().updateObject(mb);
			

			//处理文件上传

			if(files!=null)
				for(int i=0;i<files.size();i++){//控制上传部分

					String extend_name=getUpload_fileFileName()[i];
					System.out.println(extend_name);
					int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
					extend_name=extend_name.substring(extend_name_int);
								
					java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
				   String datestr = df.format(new java.util.Date()) ;
				   Random rand = new Random();//生成随机数
				   int ram = rand.nextInt(500);
					String ram_str = String.valueOf(ram);
					for(int i_i=0; ram_str.length()<6;i_i++){
						ram_str="0"+ram_str;
					}
					
					extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
					String Save_path="\\messageBoardFiles\\"+extend_name;//文件在服务器保存的相对路径
					
					FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
					
					FileInputStream fis=new FileInputStream(files.get(i));
					byte[] buffer=new byte[1024];
					int len=0;
					while((len=fis.read(buffer))>0){
					fos.write(buffer,0,len);
					
					}
					
				    MessageBoardFile mfile = new MessageBoardFile();
					
					System.out.println();
					System.out.println("----------第--"+(i+1)+"--文件--------------");
					System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
					System.out.println("--------文件名:"+files_title[i]+"--------------");
					System.out.println("--------文件说明:"+files_content[i]+"--------------");
					
					mfile.setFileUrl(Save_path);
					mfile.setFileName(files_title[i]);
					mfile.setFileContext(files_content[i]);
					mfile.setMessageBoard(mb);
					mfile.setAuditing("0");
					mfile.setFileDownTime(0);
					
					//插入附件表
					b2=this.getCommonService().saveObject(mfile);
			
			//文件上传处理结束
			
	        	}
		 
		 return "TeacherMessageBoardModify";
	 }
	 
	 
	 /***
	  *  显示修改留言页面
	  * @return
	  * @throws Exception
	  */
	 @SuppressWarnings("unchecked")
	 
	public String ModifyShow() throws Exception{
		 
		 
		 int id = teacherMessageBoard.getMessageBoardid();
		 
		 System.out.println("---"+id);
		 
		 String hql ="from MessageBoardFile mbf where mbf.messageBoard.messageBoardid="+id;

		 System.out.println("---"+hql);
		 
		 teacherMessageBoard=(MessageBoard) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.MessageBoard");
		 
		 
		 //附件
		 
		  messageBoardFile = this.getCommonService().findListByHql(hql, null);
		 
		 
		 
		 return "TeacherMessageBoardModifyShow";
	 }
	 
	 
	 /***
	  *  显示留言审核
	  * 
	  * @return
	  * @throws Exception
	  */
	 
	 public String AuditingBoard()throws Exception{
		
		    String hql ="from MessageBoard ";
			
			nextPage = "teacherMessageBoardAuditingBoard.action";
			
	 	  if(!"".equals(where_value)&&where_value!=null){
	    		  
	    		  System.out.println("++++-- "+where_value);
	    		  if(like.equals("like"))
	    		  {
	             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
	    		  }else{
	    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
	    		  }
	    		  
	    	  }
	 	  
	 	  if(!"".equals(auditing)&&auditing!=null){
	 		  
	 		  hql = hql +" where auditing = '"+auditing+" '";
	 	  }
	 	  
	 	  
	       hql = hql +"order by auditing,messageBoardTime,messageBoardid desc";
			
	       System.out.println("====="+hql);
			
			pm=(PageModel) this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
			
		 
		 return "TeacherMessageBoardAuditingBoard";
	 }
	 
	 
	 
	 public String AuditingBoard1()throws Exception{
			
		    String hql ="from MessageBoard ";
			
			nextPage = "teacherMessageBoardAuditingBoard.action";
			
	 	  if(!"".equals(where_value)&&where_value!=null){
	    		  
	    		  System.out.println("++++-- "+where_value);
	    		  if(like.equals("like"))
	    		  {
	             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
	    		  }else{
	    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
	    		  }
	    		  
	    	  }
	 	  
	 	  if(!"".equals(auditing)&&auditing!=null){
	 		  
	 		  hql = hql +" where auditing = '"+auditing+" '";
	 	  }
	 	  
	 	  
	       hql = hql +"order by auditing,messageBoardTime,messageBoardid desc";
			
	       System.out.println("====="+hql);
			
			pm=(PageModel) this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
			
		 
		 return "TeacherMessageBoardAuditingBoard1";
	 }

	 /***
	  * 审核为院内显示
	  * @return
	  * @throws Exception
	  */
	 public String Inside() throws Exception{
		 
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		 
		 System.out.println("id===="+teacherMessageBoard.getMessageBoardid());
		 
		 MessageBoard mb = (MessageBoard)this.getCommonService().findByIntId(teacherMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.MessageBoard");
		 
	 	 mb.setAuditing("1");
		 
		 try {
			this.getCommonService().updateObject(mb);
			
			System.out.println("审核为院内");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	   return "TeacherMessageBoardInside";
	 }
		  
	    /***
	     *   Outside 留言对外公布
	     */
	    public String Outside() throws Exception{
			  
	    	 System.out.println("id===="+teacherMessageBoard.getMessageBoardid());
			 
			 MessageBoard mb = (MessageBoard)this.getCommonService().findByIntId(teacherMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.MessageBoard");
			 
		 	 mb.setAuditing("2");
			 
			 try {
				this.getCommonService().updateObject(mb);
				
				System.out.println("审核为院外");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	    	  
	       return "TeacherMessageBoardOutside";
		 }
		
		/***
		 * 
		 *    Notallow 留言不通过
		 */
	    public String Notallow() throws Exception{
			  
	    	 System.out.println("id===="+teacherMessageBoard.getMessageBoardid());
			 
			 MessageBoard mb = (MessageBoard)this.getCommonService().findByIntId(teacherMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.MessageBoard");
			 
		 	 mb.setAuditing("3");
			 
			 try {
				this.getCommonService().updateObject(mb);
				
				System.out.println("审核为院内");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	  	  
			  return "TeacherMessageBoardNotallow";
		  }
		
		/***
		 * 
		 *    Rollback 留言未审核
		 */
	    public String Rollback() throws Exception{
			  
	    	 System.out.println("id===="+teacherMessageBoard.getMessageBoardid());
			 
			 MessageBoard mb = (MessageBoard)this.getCommonService().findByIntId(teacherMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.MessageBoard");
			 
		 	 mb.setAuditing("0");
			 
			 try {
				this.getCommonService().updateObject(mb);
				
				System.out.println("审核为院内");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	  		  return "TeacherMessageBoardRollback";
	  	  }
	    
	 
	
	    /***
		  *  回复审核为院内显示
		  * @return
		  * @throws Exception
		  */
		 public String  ReplyInside() throws Exception{
			 
			 String hql = " from  MessageBoardReply mbr where ";
			 
			 //审核所有回复
			 if(teacherMessageBoard!=null&&"1".equals(replyAll)){
				 
				 hql = hql+" mbr.messageBoard.messageBoardid = "+teacherMessageBoard.getMessageBoardid();
				 
			 }else{
				 hql =hql +" 1 = 1 ";
			 }
			 if(messageReply!=null){
				 
				 hql =hql +" and mbr.messageBoardReplyId = "+ messageReply.getMessageBoardReplyId();
			 }
			 System.out.println("--hql---"+hql);
			 
			 List replyList = this.getCommonService().findListByHql(hql, null);
			 
			 Iterator iter = replyList.iterator();
			 
			 while(iter.hasNext()){
				 
				 MessageBoardReply mbr = (MessageBoardReply)iter.next();
				 
				 mbr.setAuditing("1");
				 
				 try {
					 
					this.getCommonService().updateObject(mbr);
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			 }

			 
		   return "TeacherMessageBoardReplyInside";
		 }
			  
		    /***
		     *   Outside  回复审核为对外公布
		     */
		    public String ReplyOutside() throws Exception{
		    	 String hql = " from  MessageBoardReply mbr where ";
				 
				 //审核所有回复
				 if(teacherMessageBoard!=null&&"1".equals(replyAll)){
					 
					 hql = hql+" mbr.messageBoard.messageBoardid = "+teacherMessageBoard.getMessageBoardid();
					 
				 }else{
					 hql =hql +" 1 = 1 ";
				 }
				 if(messageReply!=null){
					 
					 hql =hql +" and mbr.messageBoardReplyId = "+ messageReply.getMessageBoardReplyId();
				 }
				 System.out.println("--hql---"+hql);
				 
				 List replyList = this.getCommonService().findListByHql(hql, null);
				 
				 Iterator iter = replyList.iterator();
				 
				 while(iter.hasNext()){
					 
					 MessageBoardReply mbr = (MessageBoardReply)iter.next();
					 
					 mbr.setAuditing("2");
					 
					 try {
						 
						this.getCommonService().updateObject(mbr);
						
					} catch (Exception e) {
						// TODO: handle exception
					}
				 }
		    	  
		       return "TeacherMessageBoardReplyOutside";
			 }
			
			/***
			 * 
			 *    Notallow 回复审核为不通过
			 */
		    public String ReplyNotallow() throws Exception{
				  
		    	 String hql = " from  MessageBoardReply mbr where ";
				 
				 //审核所有回复
				 if(teacherMessageBoard!=null&&"1".equals(replyAll)){
					 
					 hql = hql+" mbr.messageBoard.messageBoardid = "+teacherMessageBoard.getMessageBoardid();
					 
				 }else{
					 hql =hql +" 1 = 1 ";
				 }
				 if(messageReply!=null){
					 
					 hql =hql +" and mbr.messageBoardReplyId = "+ messageReply.getMessageBoardReplyId();
				 }
				 System.out.println("--hql---"+hql);
				 
				 List replyList = this.getCommonService().findListByHql(hql, null);
				 
				 Iterator iter = replyList.iterator();
				 
				 while(iter.hasNext()){
					 
					 MessageBoardReply mbr = (MessageBoardReply)iter.next();
					 
					 mbr.setAuditing("3");
					 
					 try {
						 
						this.getCommonService().updateObject(mbr);
						
					} catch (Exception e) {
						// TODO: handle exception
					}
				 }
				  return "TeacherMessageBoardReplyNotallow";
			  }
			
			/***
			 * 
			 *    Rollback 回复审核为未审核
			 */
		    public String ReplyRollback() throws Exception{
				  
		    	 String hql = " from  MessageBoardReply mbr where ";
				 
				 //审核所有回复
				 if(teacherMessageBoard!=null&&"1".equals(replyAll)){
					 
					 hql = hql+" mbr.messageBoard.messageBoardid = "+teacherMessageBoard.getMessageBoardid();
					 
				 }else{
					 hql =hql +" 1 = 1 ";
				 }
				 if(messageReply!=null){
					 
					 hql =hql +" and  mbr.messageBoardReplyId = "+ messageReply.getMessageBoardReplyId();
				 }
				 System.out.println("--hql---"+hql);
				 
				 List replyList = this.getCommonService().findListByHql(hql, null);
				 
				 Iterator iter = replyList.iterator();
				 
				 while(iter.hasNext()){
					 
					 MessageBoardReply mbr = (MessageBoardReply)iter.next();
					 
					 mbr.setAuditing("0");
					 
					 try {
						 
						this.getCommonService().updateObject(mbr);
						
					} catch (Exception e) {
						// TODO: handle exception
					}
				 }
		  		  return "TeacherMessageBoardReplyRollback";
		  	  }
		   /***
		    * 列表本人发表的留言
		    * @return
		    * @throws Exception
		    */
		    public String ListSelf()throws Exception{
		    	
		      teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		      
		      String  hql ="from  MessageBoard mb where mb.teacher.teacherId = "+teacher.getTeacherId();
		      
		      nextPage = "teacherMessageBoardListSelf.action";
		      
		      pm = this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
		      
		    	
		    	return "TeacherMessageBoardListSelf";
		    }
		    /***
		     * 显示修改本人发表的留言
		     * @return
		     * @throws Exception
		     */
		    public String ModifySelfShow()throws Exception{
		    	
		    	 
				 int id = teacherMessageBoard.getMessageBoardid();
				 
				 System.out.println("---"+id);
				 
				 String hql ="from MessageBoardFile mbf where mbf.messageBoard.messageBoardid="+id;

				 System.out.println("---"+hql);
				 
				 teacherMessageBoard=(MessageBoard) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.MessageBoard");
				 
				 //附件
				 
				  messageBoardFile = this.getCommonService().findListByHql(hql, null);
				 
		    	
		    	return "TeacherMessageBoardModifySelfShow";
		    }
		    
		    /***
		     * 修改本人发表的留言
		     * @return
		     * @throws Exception
		     */
		    public String ModifySelf()throws Exception{
		    	 System.out.println("++++");
				 
				   List<File> files=getUpload_file();
					
					List file_list = new ArrayList();
					
					setSavePath("");//上传路径
					
					boolean  b = false;
					boolean  b2 =false;
					
					System.out.println(teacherMessageBoard.getMessageBoardid());
					
					System.out.println(teacherMessageBoard.getMessageTitle());
					
					System.out.println(teacherMessageBoard.getMessageContext());
					
					int id =teacherMessageBoard.getMessageBoardid();
					
					MessageBoard mb =(MessageBoard)this.getCommonService().findByIntId(id,"gdut.jmxywz.been.MessageBoard");
					
					mb.setMessageTitle(teacherMessageBoard.getMessageTitle());
					mb.setMessageContext(teacherMessageBoard.getMessageContext());
					mb.setReplylocked(teacherMessageBoard.getReplylocked());
					
					b=this.getCommonService().updateObject(mb);
					

					//处理文件上传

					if(files!=null)
						for(int i=0;i<files.size();i++){//控制上传部分

							String extend_name=getUpload_fileFileName()[i];
							System.out.println(extend_name);
							int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
							extend_name=extend_name.substring(extend_name_int);
										
							java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
						   String datestr = df.format(new java.util.Date()) ;
						   Random rand = new Random();//生成随机数
						   int ram = rand.nextInt(500);
							String ram_str = String.valueOf(ram);
							for(int i_i=0; ram_str.length()<6;i_i++){
								ram_str="0"+ram_str;
							}
							
							extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
							String Save_path="\\messageBoardFiles\\"+extend_name;//文件在服务器保存的相对路径
							
							FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
							
							FileInputStream fis=new FileInputStream(files.get(i));
							byte[] buffer=new byte[1024];
							int len=0;
							while((len=fis.read(buffer))>0){
							fos.write(buffer,0,len);
							
							}
							
						    MessageBoardFile mfile = new MessageBoardFile();
							
							System.out.println();
							System.out.println("----------第--"+(i+1)+"--文件--------------");
							System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
							System.out.println("--------文件名:"+files_title[i]+"--------------");
							System.out.println("--------文件说明:"+files_content[i]+"--------------");
							
							mfile.setFileUrl(Save_path);
							mfile.setFileName(files_title[i]);
							mfile.setFileContext(files_content[i]);
							mfile.setMessageBoard(mb);
							mfile.setAuditing("0");
							mfile.setFileDownTime(0);
							
							//插入附件表
							b2=this.getCommonService().saveObject(mfile);
					
					//文件上传处理结束
					
			        	}
				 
		    	
		    	return "TeacherMessageBoardModifySelf";
		    }
		    /***
		     * 删除本人发表的留言
		     * @return
		     * @throws Exception
		     */
		    public String DeleteSelf()throws Exception{
		    	
				 String hql ="from MessageBoardReply mbr where ";
				 
				 //删除留言同时将所有回复删掉
				 if(teacherMessageBoard!=null){
					 if(teacherMessageBoard.getMessageBoardid()!=null){
						 
						 
						 //删除全部回复
						 hql=hql+" mbr.messageBoard.messageBoardid = " + teacherMessageBoard.getMessageBoardid() ;
						
						 List replyList = this.getCommonService().findListByHql(hql, null);
						 
						 Iterator iter = replyList.iterator();
						 
						 while(iter.hasNext()){
							 
							 MessageBoardReply mbr = (MessageBoardReply)iter.next();
							 
							 this.getCommonService().deleteObject(mbr);
							 
							 System.out.println("所有回复删除成功"); 
						 }
						 
						 this.getCommonService().deleteObject(teacherMessageBoard);
						 
					 }
				 }
		    	
		    	return "TeacherMessageBoardDeleteSelf";
		    }
		    /***
		     * 
		     * 锁定回复，不予他人回复
		     * @return
		     * @throws Exception
		     */
		    public String Replylocked()throws Exception{
		    	
				System.out.println(teacherMessageBoard.getMessageBoardid());
				
				int id =teacherMessageBoard.getMessageBoardid();
				
				MessageBoard mb =(MessageBoard)this.getCommonService().findByIntId(id,"gdut.jmxywz.been.MessageBoard");
				
				mb.setReplylocked("0");
				
			    this.getCommonService().updateObject(mb);
		    	
		    	return "TeacherMessageBoardReplylocked";
		    }
 
	    
	
}
